package com.hw.springcloud.controller;


import com.hw.springcloud.entities.CommonResult;
import com.hw.springcloud.entities.Payment;
import com.hw.springcloud.lb.LoadBalancer;
import lombok.AllArgsConstructor;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;

/**
 * @Classname: orderController
 * @Description: 描述
 * @Date: 2021/6/9
 * @Author: huwei
 */
@RestController
@RequestMapping("/order")
@AllArgsConstructor
public class OrderController {

    /**
     *
     只有一个服务时可以写地址
     */
    // private static final String URL = "http://localhost:8001";

    /**
     * 如果有多个服务就要写eureka的地址
     */
    private static final String URL = "http://CLOUD-PAYMENT-SERVICE";

    private final RestTemplate restTemplate;

    private final LoadBalancer loadBalancer;

    private final DiscoveryClient discoveryClient;

    /**
     * restTemplate.postForObject方法
     *
     * @param payment
     * @return
     */
    @GetMapping("/create")
    public CommonResult<Payment> create(Payment payment) {
        return restTemplate.postForObject(URL + "/payment/create", payment, CommonResult.class);
    }

    /**
     * restTemplate.postForEntity方法
     *
     * @param payment
     * @return
     */
    @GetMapping("/create/forEntity")
    public CommonResult<Payment> createForEntity(Payment payment) {
        ResponseEntity<CommonResult> responseEntity = restTemplate.postForEntity(URL + "/payment/create", payment, CommonResult.class);
        // 判断返回码是不是成功
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            return responseEntity.getBody();
        } else {
            return new CommonResult<>(500, "操作失败");
        }
    }

    /**
     * restTemplate调用请求
     * 使用restTemplate.getForObject方法调用别的服务的get请求方法,返回的结果是json格式
     *
     * @param id
     * @return
     */
    @GetMapping("/getById/{id}")
    public CommonResult<Payment> getById(@PathVariable("id") Long id) {
        return restTemplate.getForObject(URL + "/payment/getById/" + id, CommonResult.class);
    }

    /**
     * restTemplate调用请求
     * 使用restTemplate.getForEntity方法调用别的服务的get请求方法,返回的结果是ResponseEntity格式
     * getForEntity方法的返回结果比getForObject方法的返回结果要多出一些信息(比如:状态码,响应头,响应体等等)
     *
     * @param id
     * @return
     */
    @GetMapping("/getById/forEntity/{id}")
    public CommonResult<Payment> getByIdForEntity(@PathVariable Long id) {
        ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(URL + "/payment/getById/" + id, CommonResult.class);
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            return responseEntity.getBody();
        } else {
            return new CommonResult<>(500, "查询失败");
        }
    }

    /**
     * 测试轮询算法
     */
    @GetMapping("/payment/lb")
    public String getPaymentLb() {
        // 查询名称为CLOUD-PAYMENT-SERVICE的服务集合
        List<ServiceInstance> serviceInstanceList = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        // 轮询算法获得服务实例
        ServiceInstance serviceInstance = loadBalancer.serviceInstance(serviceInstanceList);
        // 根据实例发送请求,返回端口号,根据端口号判断轮询算法是否成功
        URI uri = serviceInstance.getUri();
        return restTemplate.getForObject(uri + "/payment/lb", String.class);
    }

    /**
     * 链路监控测试
     */
    @GetMapping("/zipkin")
    public String Zipkin(){
        String forObject = restTemplate.getForObject(URL + "/payment/zipkin", String.class);
        return forObject;
    }
}
